﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

//using DevExpress.XtraBars.Ribbon;
using WHC.OrderWater.Commons;
using WHC.Security.Common;
using WeifenLuo.WinFormsUI.Docking;
using System.Reflection;

namespace WHC.Security.UI
{
    public partial class MainForm : Form
    {
        private Dictionary<string, string> m_dicFormText2FormTypeName = new Dictionary<string, string>();
        private static MainForm m_Frm_Main = null;
        private Frm_MainToolWindow mainToolWin = null;
        public MainForm()
        {
            InitializeComponent();

            //InitLocalization();
            //InitSkinGallery();
            //InitForms();
        }

        private void InitLocalization()
        {
            //try
            //{
            //    DevExpress.Accessibility.AccLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressUtilsLocalizationCHS();
            //    DevExpress.XtraBars.Localization.BarLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraBarsLocalizationCHS();
            //    //DevExpress.XtraCharts.Localization.ChartLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraChartsLocalizationCHS();
            //    DevExpress.XtraEditors.Controls.Localizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraEditorsLocalizationCHS();
            //    //DevExpress.XtraGrid.Localization.GridLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraGridLocalizationCHS();
            //    //DevExpress.XtraLayout.Localization.LayoutLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraLayoutLocalizationCHS();
            //    //DevExpress.XtraNavBar.NavBarLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraNavBarLocalizationCHS();
            //    //DevExpress.XtraPivotGrid.Localization.PivotGridLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraPivotGridLocalizationCHS();
            //    DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraPrintingLocalizationCHS();
            //    //DevExpress.XtraReports.Localization.ReportLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraReportsLocalizationCHS();
            //    //DevExpress.XtraRichEdit.Localization.XtraRichEditLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraRichEditLocalizationCHS();
            //    //DevExpress.XtraRichEdit.Localization.RichEditExtensionsLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraRichEditExtensionsLocalizationCHS();
            //    //DevExpress.XtraScheduler.Localization.SchedulerLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraSchedulerLocalizationCHS();
            //    //DevExpress.XtraScheduler.Localization.SchedulerExtensionsLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraSchedulerExtensionsLocalizationCHS();
            //    //DevExpress.XtraSpellChecker.Localization.SpellCheckerLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraSpellCheckerLocalizationCHS();
            //    //DevExpress.XtraTreeList.Localization.TreeListLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraTreeListLocalizationCHS();
            //    //DevExpress.XtraVerticalGrid.Localization.VGridLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraVerticalGridLocalizationCHS();
            //    //DevExpress.XtraWizard.Localization.WizardLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraWizardLocalizationCHS();
            //}
            //catch (Exception ex)
            //{
            //    LogTextHelper.WriteLine(ex.Message);
            //    MessageDxUtil.ShowError(ex.Message);
            //}
        }

        //void InitSkinGallery()
        //{
        //    DevExpress.XtraBars.Helpers.SkinHelper.InitSkinGallery(rgbiSkins, true);
        //}

        private void InitForms()
        {
            #region 初始化系统名称
            try
            {
                AppConfig config = new AppConfig();
                string Manufacturer = config.AppConfigGet("Manufacturer");
                string ApplicationName = "权限管理系统";//config.AppConfigGet("ApplicationName");
                this.Text = string.Format("{0}-{1}", Manufacturer, ApplicationName);

            }
            catch
            { }
            #endregion

            ChildWinManagement.LoadMdiForm(this, typeof(FrmUser));
            ChildWinManagement.LoadMdiForm(this, typeof(FrmOU));
            ChildWinManagement.LoadMdiForm(this, typeof(FrmRole));
            ChildWinManagement.LoadMdiForm(this, typeof(FrmFunction));
        }
        private DockContent FindDocument(string text)
        {
            if (dockPanel1.DocumentStyle == DocumentStyle.DockingMdi)
            {
                foreach (Form form in MdiChildren)
                {
                    if (form.Text == text)
                    {
                        return form as DockContent;
                    }
                }

                return null;
            }
            else
            {
                foreach (DockContent content in dockPanel1.Documents)
                {
                    if (content.DockHandler.TabText == text)
                    {
                        return content;
                    }
                }

                return null;
            }
        }
        private Object ShowContentEx(string caption)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
     
            Type typForm = assembly.GetType(m_dicFormText2FormTypeName[caption]);
            //关于InvokeMember用法，不明白的可以查看：http://msdn.microsoft.com/zh-cn/library/de3dhzwy(VS.80).aspx
            Object mdiChild = typForm.InvokeMember(
               null,
                BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance,
               null,
               null,
               null);

            return mdiChild;
        }

        private Object ShowContentEx2(string strTemp)
        {

            Object mdiChild = null;
            switch (strTemp)
            {
                case "用户管理":
                    {
                        mdiChild = new FrmUser();
                        break;
                    }
                case "组织机构管理":
                    {
                        mdiChild = new FrmOU();
                        break;
                    }
                case "角色管理":
                    {
                        mdiChild = new FrmRole();
                        break;
                    }
                case "功能管理":
                    {
                        mdiChild = new FrmFunction();
                        break;
                    }

                default:
                    break;
            }
            return mdiChild;
        }
        public DockContent ShowContent(string caption/*, Type formType*/)
        {

            DockContent frm = FindDocument(caption);

            if (frm == null)
            {



                Object mdiChild = ShowContentEx2(caption);

                if (mdiChild != null)
                {
                    frm = mdiChild as DockContent;
                    frm.MdiParent = this;
                    frm.WindowState = FormWindowState.Maximized;

                }
            }

            frm.Show(this.dockPanel1);
            frm.Focus();
            frm.BringToFront();
            return frm;
        }
        public static MainForm GetMainForm()
        {
            if (m_Frm_Main == null)
            {
                m_Frm_Main = new MainForm();
            }

            return m_Frm_Main;
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            m_dicFormText2FormTypeName.Clear();
            m_dicFormText2FormTypeName.Add("用户管理", "WHC.Security.UI.FrmUser");
            m_dicFormText2FormTypeName.Add("组织机构管理", "WHC.Security.UI.FrmOU");
            m_dicFormText2FormTypeName.Add("角色管理", "WHC.Security.UI.FrmRole");
            m_dicFormText2FormTypeName.Add("功能管理", "WHC.Security.UI.FrmFunction");
            mainToolWin = new Frm_MainToolWindow();
            mainToolWin.Show(this.dockPanel1, DockState.DockLeft);
            this.dockPanel1.DockLeftPortion = 0.17;
        }

        private void toolStripButtonUser_Click(object sender, EventArgs e)
        {
            ToolStripButton tsmi = (ToolStripButton)sender;
            ShowContent(tsmi.Text.ToString());
        }

        private void toolStripButtonOU_Click(object sender, EventArgs e)
        {
            ToolStripButton tsmi = (ToolStripButton)sender;
            ShowContent(tsmi.Text.ToString());
        }

        private void toolStripButtonRole_Click(object sender, EventArgs e)
        {
            ToolStripButton tsmi = (ToolStripButton)sender;
            ShowContent(tsmi.Text.ToString());
        }

        private void toolStripButtonFun_Click(object sender, EventArgs e)
        {
            ToolStripButton tsmi = (ToolStripButton)sender;
            ShowContent(tsmi.Text.ToString());
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Close();
            m_Frm_Main = null;
            //Application.Exit();
        }
    }
}
